package com.example.italkapp2.model;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.Display;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.example.italkapp2.controller.activity.InviteActivity;
import com.example.italkapp2.model.bean.GroupInfo;
import com.example.italkapp2.model.bean.InvitationInfo;
import com.example.italkapp2.model.bean.UserInfo;
import com.example.italkapp2.model.dao.InviteTable;
import com.example.italkapp2.model.db.DBHelper;
import com.example.italkapp2.utils.Constant;
import com.example.italkapp2.utils.SpUtils;
import com.hyphenate.EMContactListener;
import com.hyphenate.EMGroupChangeListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMucSharedFile;
import com.hyphenate.easeui.interfaces.EaseGroupListener;

import java.util.List;

//全局事件监听类
public class EventListener {
    private Context mContext;
    private final LocalBroadcastManager mLBM;

    public EventListener(Context context){
        mContext=context;

        //创建一个发送广播的管理者对象
        mLBM = LocalBroadcastManager.getInstance(mContext);


        //注册一个联系人变化的监听事件
        EMClient.getInstance().contactManager().setContactListener(emContactListener);

        //注册一个群信息变化的监听
        EMClient.getInstance().groupManager().addGroupChangeListener(emGroupChangeListener);


    }

    private final EMGroupChangeListener emGroupChangeListener = new EMGroupChangeListener() {

        //接收到群组加入邀请
        @Override
        public void onInvitationReceived(String groupId, String groupName, String inviter, String reason) {
            // 数据更新
            InvitationInfo invitationInfo = new InvitationInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroupInfo(new GroupInfo(groupName, groupId, inviter));
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.NEW_GROUP_INVITE);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            // 红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);

            // 发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        //用户申请加入群
        @Override
        public void onRequestToJoinReceived(String groupId, String groupName, String applicant, String reason) {
            // 数据更新
            InvitationInfo invitationInfo = new InvitationInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroupInfo(new GroupInfo(groupName, groupId, applicant));
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.NEW_GROUP_APPLICATION);
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            // 红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);

            // 发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        //加群申请被同意
        @Override
        public void onRequestToJoinAccepted(String groupId, String groupName, String accepter) {
            // 更新数据
            InvitationInfo invitationInfo = new InvitationInfo();
            invitationInfo.setGroupInfo(new GroupInfo(groupName,groupId,accepter));
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.GROUP_APPLICATION_ACCEPTED);

            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            // 红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);

            // 发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        //加群申请被拒绝
        @Override
        public void onRequestToJoinDeclined(String groupId, String groupName, String decliner, String reason) {
            // 更新数据
            InvitationInfo invitationInfo = new InvitationInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroupInfo(new GroupInfo(groupName, groupId, decliner));
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.GROUP_APPLICATION_DECLINED);

            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            // 红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);

            // 发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        //群组邀请被同意
        @Override
        public void onInvitationAccepted(String groupId, String invitee, String reason) {
            // 更新数据
            InvitationInfo invitationInfo = new InvitationInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroupInfo(new GroupInfo(groupId, groupId, invitee));
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED);

            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            // 红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);

            // 发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        //群组邀请被拒绝
        @Override
        public void onInvitationDeclined(String groupId, String invitee, String reason) {
            // 更新数据
            InvitationInfo invitationInfo = new InvitationInfo();
            invitationInfo.setReason(reason);
            invitationInfo.setGroupInfo(new GroupInfo(groupId, groupId, invitee));
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.GROUP_INVITE_DECLINED);

            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            // 红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);

            // 发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        //收到 群成员被删除
        @Override
        public void onUserRemoved(String groupId, String groupName) {

        }

        //收到 群被解散
        @Override
        public void onGroupDestroyed(String groupId, String groupName) {

        }

        //接收邀请时自动加入到群组的通知
        @Override
        public void onAutoAcceptInvitationFromGroup(String groupId, String inviter, String inviteMessage) {
            // 更新数据
            InvitationInfo invitationInfo = new InvitationInfo();
            invitationInfo.setReason(inviteMessage);
            invitationInfo.setGroupInfo(new GroupInfo(groupId, groupId, inviter));
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.GROUP_INVITE_ACCEPTED);

            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            // 红点处理
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE, true);

            // 发送广播
            mLBM.sendBroadcast(new Intent(Constant.GROUP_INVITE_CHANGED));
        }

        //成员禁言的通知
        @Override
        public void onMuteListAdded(String groupId, List<String> mutes, long muteExpire) {

        }

        //成员从禁言列表里移除通知
        @Override
        public void onMuteListRemoved(String groupId, List<String> mutes) {

        }

        //成员被加到白名单中
        @Override
        public void onWhiteListAdded(String groupId, List<String> whitelist) {

        }

        //成员从白名单中被移除
        @Override
        public void onWhiteListRemoved(String groupId, List<String> whitelist) {

        }

        //全员禁言是否开启
        @Override
        public void onAllMemberMuteStateChanged(String groupId, boolean isMuted) {

        }

        //增加管理员的通知
        @Override
        public void onAdminAdded(String groupId, String administrator) {

        }

        //管理员移除的通知
        @Override
        public void onAdminRemoved(String groupId, String administrator) {

        }

        //群所有者变动通知
        @Override
        public void onOwnerChanged(String groupId, String newOwner, String oldOwner) {

        }

        //群组加入新成员通知
        @Override
        public void onMemberJoined(String groupId, String member) {

        }

        //群成员退出通知
        @Override
        public void onMemberExited(String groupId, String member) {

        }

        //群公告变动通知
        @Override
        public void onAnnouncementChanged(String groupId, String announcement) {

        }

        //增加共享文件的通知
        @Override
        public void onSharedFileAdded(String groupId, EMMucSharedFile sharedFile) {

        }

        //群共享文件删除通知
        @Override
        public void onSharedFileDeleted(String groupId, String fileId) {

        }
    };

    private final EMContactListener emContactListener=new EMContactListener(){

        //增加了联系人时回调此方法
        @Override
        public void onContactAdded(String username) {
            //联系人增加，数据库更新
            Model.getInstance().getDbManager().getContactTableDao().saveContact(new UserInfo(username),true);

            //发送联系人变化的全局广播，有需要的事物会处理
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_CHANGED));
        }

        //被删除时回调此方法
        @Override
        public void onContactDeleted(String username) {
            //联系人删除，数据库更新
            Model.getInstance().getDbManager().getContactTableDao().deleteContactByHxId(username);
            Model.getInstance().getDbManager().getInviteTableDao().removeInvitation(username);

            //发送联系人变化的广播，有需要的事物会处理
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_CHANGED));
        }

        //收到好友邀请
        @Override
        public void onContactInvited(String username, String reason) {
            //接收邀请，数据库更新
            InvitationInfo invitationInfo=new InvitationInfo();
            invitationInfo.setUser(new UserInfo(username));
            invitationInfo.setReason(reason);
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.NEW_INVITE);//接收状态为新邀请

            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            List<InvitationInfo> invitationInfos=Model.getInstance().getDbManager().getInviteTableDao().getInvitations();
            for(InvitationInfo invitationInfo1:invitationInfos){
                System.out.println(invitationInfo1.toString());
            }

            //对新邀请进行小红点提示
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);

            //发送邀请信息变化的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
        }


        //好友请求被同意
        @Override
        public void onFriendRequestAccepted(String username) {
            //数据库更新
            InvitationInfo invitationInfo=new InvitationInfo();
            invitationInfo.setUser(new UserInfo(username));
            invitationInfo.setStatus(InvitationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER);//接收状态为别人同意了你的邀请
            Model.getInstance().getDbManager().getInviteTableDao().addInvitation(invitationInfo);

            //进行小红点提示
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);

            //发送邀请信息变化的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
        }

        //好友请求被拒绝
        @Override
        public void onFriendRequestDeclined(String username) {
            //进行小红点提示
            SpUtils.getInstance().save(SpUtils.IS_NEW_INVITE,true);

            //发送邀请信息变化的广播
            mLBM.sendBroadcast(new Intent(Constant.CONTACT_INVITE_CHANGED));
        }

    };
}
